<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="70%" append-to-body>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="宗地序号" prop="landId">
            <el-input v-model="formData.landId" placeholder="请输入宗地序号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="权利人名称" prop="obligeeName">
            <el-input v-model="formData.obligeeName" placeholder="请输入权利人名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="权利人类型" prop="obligeeType">
            <el-select v-model="formData.obligeeType" placeholder="请选择权利人类型">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.OBLIGEE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="权利人证件类型" prop="obligeeCertificateType">
            <el-select v-model="formData.obligeeCertificateType" placeholder="请选择权利人证件类型">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.ID_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="权利人证件号" prop="obligeeCertificate">
            <el-input v-model="formData.obligeeCertificate" placeholder="请输入权利人证件号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="权利人电话" prop="obligeePhone">
            <el-input v-model="formData.obligeePhone" placeholder="请输入权利人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="权利人地址" prop="obligeeAddress">
            <el-input v-model="formData.obligeeAddress" placeholder="请输入权利人地址" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="法人名称" prop="corporationName">
            <el-input v-model="formData.corporationName" placeholder="请输入法人名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="法人证件类型" prop="corporationCertificateType">
            <el-select
              v-model="formData.corporationCertificateType"
              placeholder="请选择法人证件类型"
            >
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.ID_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="法人证件号" prop="corporationCertificate">
            <el-input v-model="formData.corporationCertificate" placeholder="请输入法人证件号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="经办人" prop="obligeeLiasion">
            <el-input v-model="formData.obligeeLiasion" placeholder="请输入经办人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="经办人电话" prop="liasionPhone">
            <el-input v-model="formData.liasionPhone" placeholder="请输入经办人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="formData.status">
              <el-radio
                v-for="dict in getStrDictOptions(DICT_TYPE.SYS_STATUS)"
                :key="dict.value"
                :value="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="权利人档案" name="obligeeFile">
        <ObligeeFileForm ref="obligeeFileFormRef" :belong-id="formData.id" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { ObligeeApi, ObligeeVO } from '@/api/zszx/obligee'
import ObligeeFileForm from './components/ObligeeFileForm.vue'

/** 权利人信息 表单 */
defineOptions({ name: 'ObligeeForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  landId: undefined,
  userId: undefined,
  obligeeName: undefined,
  obligeeType: undefined,
  obligeeCertificateType: undefined,
  obligeeCertificate: undefined,
  obligeePhone: undefined,
  obligeeAddress: undefined,
  corporationName: undefined,
  corporationCertificateType: undefined,
  corporationCertificate: undefined,
  obligeeLiasion: undefined,
  liasionPhone: undefined,
  remark: undefined,
  status: undefined
})
const formRules = reactive({
  landId: [{ required: true, message: '宗地序号不能为空', trigger: 'blur' }],
  obligeeName: [{ required: true, message: '权利人名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('obligeeFile')
const obligeeFileFormRef = ref()

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ObligeeApi.getObligee(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await obligeeFileFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'obligeeFile'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ObligeeVO
    // 拼接子表的数据
    data.obligeeFiles = obligeeFileFormRef.value.getData()
    if (formType.value === 'create') {
      await ObligeeApi.createObligee(data)
      message.success(t('common.createSuccess'))
    } else {
      await ObligeeApi.updateObligee(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    landId: undefined,
    userId: undefined,
    obligeeName: undefined,
    obligeeType: undefined,
    obligeeCertificateType: undefined,
    obligeeCertificate: undefined,
    obligeePhone: undefined,
    obligeeAddress: undefined,
    corporationName: undefined,
    corporationCertificateType: undefined,
    corporationCertificate: undefined,
    obligeeLiasion: undefined,
    liasionPhone: undefined,
    remark: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>
